package com.czxy.candidates.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czxy.candidates.feign.CityFeign;
import com.czxy.candidates.feign.OccupationFeign;
import com.czxy.domain.TbCandidates;
import com.czxy.candidates.service.CandidatesService;
import com.czxy.domain.TbCity;
import com.czxy.domain.TbOccupation;
import com.czxy.findjob.vo.BaseResult;
import com.czxy.vo.CandidatesVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 求职者信息表 前端控制器
 * </p>
 *
 * @author xuewen
 * @since 2022-01-13
 */
@RestController
@RequestMapping("/candidates")
@Api(tags = "求职者管理")
public class CandidatesController {

    @Resource
    CandidatesService candidatesService;

    @Resource
    OccupationFeign occupationFeign;
    @Resource
    CityFeign cityFeign;
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询用户信息")
    public BaseResult<TbCandidates> getById(@PathVariable("id") Integer id){
        TbCandidates candidates = candidatesService.getById(id);
        if (candidates.getOccupationId()!=null){
            BaseResult<TbOccupation> occupationBaseResult = occupationFeign.findById(candidates.getOccupationId());
            candidates.setTbOccupation(occupationBaseResult.getData());
            TbCity data = cityFeign.findById(candidates.getCityId()).getData();
            candidates.setTbCity(data);
        }
        return BaseResult.ok("查询成功!",candidates);
    }
    @PutMapping()
    public BaseResult updateById(@RequestBody TbCandidates tbCandidates){
        boolean updateById = candidatesService.updateById(tbCandidates);
        if (updateById){
            return BaseResult.ok("更新成功!");
        }else {
            return BaseResult.error("更新失败!");
        }
    }

    @PostMapping("/condition")
    public BaseResult condition(@RequestBody CandidatesVo candidatesVo){
       Page page =  candidatesService.condition(candidatesVo);

       return BaseResult.ok("查询成功",page);
    }
    @PostMapping("/save")
    public BaseResult<Integer> save(@RequestBody TbCandidates tbCandidates){
        tbCandidates.setCreateTime(new Date());
        tbCandidates.setUpdateTime(new Date());
        boolean save = candidatesService.save(tbCandidates);
        if (save){
            return BaseResult.ok("报存成功",tbCandidates.getId());
        }else {
            return BaseResult.error("保存失败");
        }
    }
    @GetMapping("/findAll")
    public BaseResult findAll(){
       List<TbCandidates>  list =  candidatesService.findAll();
       return BaseResult.ok("查询成功",list);
    }



}

